KISSY.add("malldetail/common/imagezoom",
	function (e, t, a, i, o) {
		var n = t.all;

		function l(e, t, a) {
			return Math.min(Math.max(e, t), a)
		}

		function r() {
			r.superclass.constructor.apply(this, arguments);
			this._init()
		}

		e.extend(r, i, {
				_init: function () {
					var e = this;
					e.on("afterBigImageWidthChange",
						function (e) {
							var t = this;
							if (!t.bigImage) {
								return
							}
							t.bigImage.width(e.newVal)
						});
					e.on("afterBigImageHeightChange",
						function (e) {
							var t = this;
							if (!t.bigImage) {
								return
							}
							t.bigImage.height(e.newVal)
						});
					e.on("afterBigImageSrcChange",
						function (t) {
							if (!e.bigImage) {
								return
							}
							e.bigImage.attr("src", t.newVal)
						})
				},
				onMouseMove: function (e) {
					var t = this,
						a = n(t.get("imageNode")),
						i = t.imageLeft,
						o = t.imageTop,
						r = t.imageWidth,
						s = e.pageX,
						d = e.pageY,
						c = t.imageHeight;
					if (s > i && s < i + r && d > o && d < o + c) {
						var m, u, p = t.lens,
							f;
						if (p) {
							m = s - t.lensWidth / 2;
							u = d - t.lensHeight / 2;
							p.offset({
								left: t.lensLeft = l(m, t.minLensLeft, t.maxLensLeft),
								top: t.lensTop = l(u, t.minLensTop, t.maxLensTop)
							})
						}
						var v = a.width(),
							h = a.height();
						f = {
							left: l(-(e.pageX - t.imageLeft) * t.zoomMultipleW + v / 2, t.minBigImageLeft, t.maxBigImageLeft),
							top: l(-(e.pageY - t.imageTop) * t.zoomMultipleH + h / 2, t.minBigImageTop, t.maxBigImageTop)
						};
						t.bigImage.css(f)
					} else {
						t.hide()
					}
				},
				start: function () {
					var e = this;
					if (e._isStart) {
						return
					}
					n(this.get("imageNode")).on("mouseenter", this.show, this);
					this._isStart = true
				},
				show: function (t) {
					var i = this,
						l = n(i.get("imageNode")),
						r = i.get("type"),
						s = l.width(),
						d = l.height();
					if (i._isShow) {
						return
					}
					i.hide();
					if (!i.Zoomer) {
						i.Zoomer = new a({
							elCls: "ks-imagezoom-viewer",
							align: i.get("align"),
							width: s,
							height: d
						});
						i.Zoomer.render();
						var c = i.Zoomer.get("contentEl"),
							m = ' style="position:absolute;top:-9999px;left:-9999px;" ';
						i.bigImage = n(e.substitute("<img src=" + "{src} {style} />", {
							src: i.get("bigImageSrc"),
							style: m
						})).appendTo(c, o);
						var u = i.imageWrap = n(e.substitute("<span class='{wrapClass}'></span>", {
							wrapClass: i.get("prefixCls") + "imagezoom-wrap"
						})).insertBefore(l);
						u.prepend(l);
						i.lens = n("<span " + m + ' class="' + i.get("prefixCls") + "imagezoom-lens" + '"></span>').appendTo(i.imageWrap, o)
					}
					var p = l.offset(),
						f,
						v,
						h,
						g,
						b,
						_,
						k,
						y = i.get("bigImageWidth"),
						S = i.get("bigImageHeight"),
						w,
						T,
						x;
					f = i.imageLeft = p.left;
					x = i.imageTop = p.top;
					v = i.imageWidth = l.width();
					h = i.imageHeight = l.height();
					g = i.zoomMultipleH = S / h;
					b = i.zoomMultipleW = y / v;
					_ = i.lensWidth = s / b;
					k = i.lensHeight = d / g;
					i.minLensLeft = f;
					i.minLensTop = x;
					i.maxLensTop = x + h - k;
					i.maxLensLeft = f + v - _;
					i.maxBigImageLeft = 0;
					i.maxBigImageTop = 0;
					i.minBigImageLeft = -(y - s);
					i.minBigImageTop = -(S - d);
					w = i.Zoomer.get("align") || {};
					T = w.node;
					delete w.node;
					w = e.clone(w);
					w.node = T || l;
					i.Zoomer.set("align", w);
					n(document).on("mousemove mouseleave", i.onMouseMove, i);
					i.onMouseMove(t);
					i.lens.css({
						width: i.lensWidth,
						height: i.lensHeight
					});
					i.lens.show();
					i.Zoomer.show();
					i._isShow = true;
					i.fire("show")
				},
				hide: function () {
					var e = this;
					if (!e._isShow) {
						return
					}
					n(document).detach("mousemove mouseleave", e.onMouseMove, e);
					e.lens && e.lens.hide();
					e.Zoomer && e.Zoomer.hide();
					this._isShow = false;
					e.fire("hide")
				},
				end: function () {
					var e = this;
					if (!e._isStart) {
						return
					}
					e.hide();
					n(e.get("imageNode")).detach("mouseenter", e.show, e);
					e._isStart = false
				},
				destroy: function () {
					var e = this;
					e.end()
				}
			},
			{
				xclass: "imagezoom-viewer"
			});
		r.ATTRS = {
			imageNode: {
				setter: function (e) {
					return t.one(e)
				}
			},
			prefixCls: {
				value: "ks-"
			},
			bigImageSrc: {
				valueFn: function () {
					return this.get("imageNode").attr("data-ks-imagezoom")
				}
			},
			bigImageWidth: {},
			bigImageHeight: {}
		};
		return r
	},
	{
		requires: ["node", "overlay", "base"]
	});
